import { createRouter,
	createWebHistory
} from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/auth/Login.vue'
import Register from '../views/auth/Register.vue'
import Live from '../views/Live.vue'
import StudentClasses from '../views/StudentClasses.vue'
import ClassDetail from '../views/ClassDetail.vue'
import Homework from '../views/homework/Homework.vue'
import Study from '../views/Study.vue'
import About from '../views/about/About.vue'
import Careers from '../views/about/Careers.vue'
import Contact from '../views/about/Contact.vue'
import FAQ from '../views/help/FAQ.vue'
import Guide from '../views/help/Guide.vue'
import Feedback from '../views/help/Feedback.vue'
import Partnership from '../views/partnership/Partnership.vue'
import Enterprise from '../views/enterprise/Enterprise.vue'
import Instructor from '../views/instructor/Instructor.vue'
import Profile from '../views/Profile.vue'
import GradeHomework from '@/views/homework/GradeHomework.vue'
import ClassAnnouncements from '../views/ClassAnnouncements.vue'
import ClassKnowledgePoints from '../views/ClassKnowledgePoints.vue'
import ClassVideo from '../views/ClassVideo.vue'
import LiveManagement from '../views/live-management.vue'
import Attendance from '../views/Attendance.vue'
import StudentMembers from '../views/StudentMembers.vue'

const routes = [{
		path: '/login',
		name: 'Login',
		component: Login,
		meta: {
			title: '登录 - 智能直播互动教育平台',
			hideNavigation: true
		}
	},
	{
		path: '/',
		name: 'Home',
		component: Home,
		meta: {
			title: 'EduLiveLink - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/home',
		redirect: '/'
	},
	{
		path: '/register',
		name: 'Register',
		component: Register,
		meta: {
			title: '注册 - 智能直播互动教育平台',
			hideNavigation: true
		}
	},
	{
		path: '/LiveManagement',
		name: 'LiveManagement',
		component: LiveManagement,
		meta: {
			title: '开启直播'
		}
	},
	{
		path: '/attendance',
		name: 'Attendance',
		component: Attendance,
		meta: {
			title: '学生签到',
			hideNavigation: true,
			skipAuth: true
		}
	},
	{
		path: '/courses',
		name: 'Courses',
		component: () => import('../views/Courses.vue'),
		meta: {
			title: '我的课程 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/student-classes',
		name: 'StudentClasses',
		component: StudentClasses,
		meta: {
			title: '我的班级 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/class/:id',
		name: 'ClassDetail',
		component: ClassDetail,
		meta: {
			title: '班级详情 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/student-members',
		name: 'StudentMembers',
		component: StudentMembers,
		meta: {
			title: '学生列表 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/class/:id/homework',
		name: 'Homework',
		component: Homework,
		meta: {
			title: '交作业 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/class/:classId/homework/grade',
		name: 'GradeHomeworkList',
		component: GradeHomework,
		meta: {
			title: '作业评价 - 智能直播互动教育平台',
			isGrading: true,
			requiresAuth: true
		}
	},
	{
		path: '/class/:classId/announcements',
		name: 'ClassAnnouncements',
		component: ClassAnnouncements,
		meta: {
			title: '班级公告 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/class/:classId/knowledgepoints',
		name: 'ClassKnowledgePoints',
		component: ClassKnowledgePoints,
		meta: {
			title: '班级知识点 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/class/:classId/videos',
		name: 'ClassVideo',
		component: ClassVideo,
		meta: {
			title: '发布视频'
		}
	},
	{
		path: '/LiveManagement',
		name: 'LiveManagement',
		component: LiveManagement,
		meta: {
			title: '开启直播'
		}
	},
	{
		path: '/resources',
		redirect: '/',
		meta: {
			title: 'EduLiveLink - 智能直播互动教育平台'
		}
	},
	{
		path: '/study',
		name: 'Study',
		component: Study,
		meta: {
			title: '开始学习 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/about',
		name: 'About',
		component: About,
		meta: {
			title: '关于我们 - 智能直播互动教育平台'
		}
	},
	{
		path: '/careers',
		name: 'Careers',
		component: Careers,
		meta: {
			title: '加入我们 - 智能直播互动教育平台'
		}
	},
	{
		path: '/contact',
		name: 'Contact',
		component: Contact,
		meta: {
			title: '联系我们 - 智能直播互动教育平台'
		}
	},
	{
		path: '/faq',
		name: 'FAQ',
		component: FAQ,
		meta: {
			title: '常见问题 - 智能直播互动教育平台'
		}
	},
	{
		path: '/profile',
		name: 'Profile',
		component: Profile,
		meta: {
			title: '个人中心 - 智能直播互动教育平台',
			requiresAuth: true
		}
	},
	{
		path: '/guide',
		name: 'Guide',
		component: Guide,
		meta: {
			title: '学习指南 - 智能直播互动教育平台'
		}
	},
	{
		path: '/feedback',
		name: 'Feedback',
		component: Feedback,
		meta: {
			title: '意见反馈 - 智能直播互动教育平台'
		}
	},
	{
		path: '/partnership',
		name: 'Partnership',
		component: Partnership,
		meta: {
			title: '机构合作 - 智能直播互动教育平台'
		}
	},
	{
		path: '/enterprise',
		name: 'Enterprise',
		component: Enterprise,
		meta: {
			title: '企业培训 - 智能直播互动教育平台'
		}
	},
	{
		path: '/instructor',
		name: 'Instructor',
		component: Instructor,
		meta: {
			title: '讲师招募 - 智能直播互动教育平台'
		}
	}
]

const router = createRouter({
	history: createWebHistory(),
	routes
})

// 路由守卫 - 设置页面标题和权限控制
router.beforeEach((to, from, next) => {
	// 设置页面标题
	if (to.meta.title) {
		document.title = to.meta.title
	}

	// 检查是否需要跳过认证
	if (to.meta.skipAuth) {
		console.log('跳过认证检查')
		next()
		return
	}

	// 检查用户认证状态
	const isAuthenticated = localStorage.getItem('userToken') || sessionStorage.getItem('userToken')

	// 如果访问登录或注册页面
	if (to.path === '/login' || to.path === '/register') {
		if (isAuthenticated) {
			// 已经登录，重定向到首页
			next('/home')
		} else {
			// 未登录，允许访问认证页面
			next()
		}
		return
	}

	// 如果需要认证的页面
	if (to.meta.requiresAuth) {
		if (!isAuthenticated) {
			// 没有登录，重定向到登录页面并保存用户想要访问的页面
			next({
				path: '/login',
				query: { redirect: to.fullPath }
			})
			return
		}
	}

	// 其他情况允许继续导航
	next()
})

export default router